﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SMS_Gateway
{
    public class SMSMisrGateway:HTTPGateway
    {
        public override bool SendSingleSMSTo(Credentials cred, SingleSMS sms, SMSContact cont, bool unicode)
        {
            string prefix = Prefix;
            var usernameField = new KeyValuePair<string, string>("username", HttpUtility.UrlEncode(cred.Username));
            var passwordField = new KeyValuePair<string, string>("password", cred.Password);
            var recepientField = new KeyValuePair<string, string>("mobile", "2" + cont.Number);
            var senderIdField = new KeyValuePair<string, string>("sender", _senderID);

           
            KeyValuePair<string, string> messageField;
            KeyValuePair<string, string> languageField;




            if (unicode)
            {
                string _hexencodedString = EncodeTo4DigitHex(sms.SMS_Body);


                messageField = new KeyValuePair<string, string>("message", _hexencodedString); // I read the manual
                languageField = new KeyValuePair<string, string>("language", "3");

            }
            else
            {

                messageField = new KeyValuePair<string, string>("message", HttpUtility.UrlEncode(sms.SMS_Body));
                languageField = new KeyValuePair<string, string>("language", "1");

            }


            string finalQuery = this.BuildQueryString(prefix, new KeyValuePair<string, string>[] { usernameField, passwordField, languageField, senderIdField , recepientField, messageField});
            string result = this.HTTPSend(finalQuery);

            if (result.Contains("1901"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public SMSMisrGateway(string senderID):base(senderID)
        {

        }

        protected override string Prefix
        {
            get { return "http://smsmisr.com/api/send.aspx"; }
        }
    }
}
